package org.goldfish.distributed_framework.config;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.Properties;
import java.util.HashMap;

public class ServerConfig {
    private String host;

    public String getHost() {
        return host;
    }

    public int getPort() {
        return port;
    }

    public String getServiceName() {
        return serviceName;
    }

    public String getServiceImpl() {
        return serviceImpl;
    }

    public String getRegistryAddress() {
        return registryAddress;
    }

    private int port;
    private String serviceName; // 短名 echo
    private String serviceImpl; // 类名 org.goldfish.distributed_framework.server.EchoService
    private String registryAddress;

    public final Map<String, String> services = new HashMap<>();

    private ServerConfig() {
        // 私有构造，必须通过 Builder 构建
    }

    public static class Builder {
        private final ServerConfig config = new ServerConfig();

        public Builder serviceName(String serviceName) {
            config.serviceName = serviceName;
            return this;
        }

        public Builder registryAddress(String registryAddress) {
            config.registryAddress = registryAddress;
            return this;
        }

        public Builder port(int port) {
            config.port = port;
            return this;
        }

        public Builder loadFromProperties() {
            config.loadServiceConfig();
            System.out.println(config.services);
            return this;
        }

        public Builder serviceImpl(String serviceImpl) {
            config.serviceImpl = serviceImpl;
            return this;
        }

        public ServerConfig build() {
            return config;
        }
    }

    // 从类路径加载额外的服务配置文件
    private void loadServiceConfig() {
        InputStream inputStream = getClass().getClassLoader().getResourceAsStream("server.properties");
        if (inputStream == null) {
            System.err.println("无法找到配置文件！" + "server.properties");
            return;
        }

        try {
            Properties properties = new Properties();
            properties.load(inputStream);

            // 读取服务相关配置项
            for (String serviceName : properties.stringPropertyNames()) {
                if (serviceName.startsWith("service.")) {
                    String serviceKey = serviceName.substring(8);  // 去掉 "service."
                    String serviceClass = properties.getProperty(serviceName);
                    services.put(serviceKey, serviceClass);
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        services.forEach(
                (key, value) -> {
                    System.out.println("Service: " + key + ", Class: " + value);
                }
        );
    }

    @Override
    public String toString() {
        return "ServerConfig{" +
                "host='" + host + '\'' +
                ", port=" + port +
                ", serviceName='" + serviceName + '\'' +
                ", registryAddress='" + registryAddress + '\'' +
                ", services=" + services +
                '}';
    }
}
